//
//  ViewController.m
//  25-多线程
//
//  Created by dllo on 16/8/17.
//  Copyright © 2016年 ZHW. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // iOS下的多线程
    // 进程和线程区别
    // 进程: 进程是用户下达执行应用程序的命令时, 所启动的东西
    // 线程: 进程的执行单位
    
    // 2 多线程
    // 为了更好的进行一些程序的操作, 节省时间, 提高效率
    // 一个进程不能过多的创建的线程, 资源消耗过大. 开辟5个左右的线程
    // 注意: 所有UI的操作 都放在主线程中执行
    // 需要开辟线程的操作, 图像文字渲染, 大数据存储, 网络请求等
    
    /*
    // 3 NSThread
    //  1> currentThread 获取当前线程
    [NSThread currentThread];
    // <NSThread: 0x608000070500>{number = 1, name = main}
    // number 线程编号 name 线程名
    NSLog(@"%@", [NSThread currentThread]);
    // 2> 开辟子线程执行操作
    // 子线程随机创建
    [NSThread detachNewThreadSelector:@selector(newThread) toTarget:self withObject:nil];
    [NSThread detachNewThreadWithBlock:^{
        NSThread *thread = [NSThread currentThread];
        NSLog(@"newThread = %@", thread);
    }];
    
    // 3> 延迟当前线程执行
//    NSDate *date = [NSDate dateWithTimeIntervalSinceNow:10];
//    [NSThread sleepUntilDate:date];
//    NSLog(@"111");
    // 4> 相对时间
//    [NSThread sleepForTimeInterval:5];
    NSLog(@"111");
    // 5> exit 退出当前线程
    // 退出后 线程后续操作不会执行
//    [NSThread exit];
    // 6> 线程优先级 默认为0.5
    double priortity = [NSThread threadPriority];
    NSLog(@"主线程优先级%lf", priortity);
    // 7> name 线程名
    [NSThread mainThread].name = @"主线程";
    // 8 isMainThread
    // 判断当前线程是否为主线程
    [NSThread isMainThread];
    // 判断线程对象是否为主线程
    [[NSThread mainThread] isMainThread];
    */
    
    // 9> 初始化方法
    // 初始化一个未启动的线程
    NSThread *newThread = [[NSThread alloc] initWithTarget:self selector:@selector(myThread) object:nil];
    [newThread start];
    
    // 子线程加载图片
//    [NSThread detachNewThreadSelector:@selector(loadImage) toTarget:self withObject:nil];
    // 10>将耗时的操作 放入后台执行
    // 不关心线程问题
    [self performSelectorInBackground:@selector(loadImage) withObject:nil];
    // 11> performSelectorOnMainThread 返回主线程
    
}

- (void)loadImage {
    NSURL *url = [NSURL URLWithString:@"http://cdn.duitang.com/uploads/item/201512/24/20151224133438_ftzBJ.jpeg"];
    NSData *imageData = [NSData dataWithContentsOfURL:url];
    [NSThread currentThread];
    NSLog(@"%@", [NSThread currentThread]);
    // 返回主线程 显示图片
    [self performSelectorOnMainThread:@selector(onMainThreadHandle:) withObject:imageData waitUntilDone:YES];
}

- (void)onMainThreadHandle:(id)object {
    _imageView.image = [UIImage imageWithData:object];
}

- (void)myThread {
    for (int i = 0; i < 100; i++) {
        [NSThread exit];
    }
    NSLog(@"%s", __FUNCTION__);
}

- (void)newThread {
    NSThread *thread = [NSThread currentThread];
    thread.name = @"rogue";
    NSLog(@"newThread = %@", thread);
    double priortity = [NSThread threadPriority];
    NSLog(@"newThread优先级%lf", priortity);
}

- (IBAction)buttonAction:(id)sender {
    for (int i = 0; i < 1000000000; i++) {
        NSLog(@"sob");
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
